<template>
  <div>
    <!-- 个人信息 -->
    <div class="visitor-info-section">
      <div class="visitor-info-header">
        <h3 class="section-title">观众信息</h3>
        <div class="required-notice">需填写 1 位观众信息</div>
      </div>
      
      <div class="visitor-form">
        <div class="visitor-card" @click="showPersonalInfoSelector">
          <div class="personal-info-prompt">
            <span class="prompt-text">{{ props.selectedPersonalInfo ? props.selectedPersonalInfo.name : '点击填写个人信息' }}</span>
            <span class="prompt-arrow">→</span>
          </div>
        </div>
      </div>
    </div>

        <!-- 观众信息选择弹窗 -->
    <el-dialog
      :model-value="personalInfoSelectorVisible"
      @update:model-value="personalInfoSelectorVisible = $event"
      title="选择观众信息"
      width="90%"
      class="personal-info-selector-dialog"
    >
    <!-- 加载状态 -->
    <div v-if="personalInfoLoading" class="loading-container">
      <div class="loading-spinner">⏳</div>
      <div class="loading-text">正在加载您的个人信息...</div>
    </div>
    
    <!-- 个人信息列表 -->
    <div v-else-if="personalInfoList.length > 0" class="personal-info-list">
      <div 
        class="personal-info-item"
        :class="{ active: selectedPersonalInfo && selectedPersonalInfo.id === info.id }"
        v-for="info in personalInfoList" 
        :key="info.id"
        @click="selectPersonalInfo(info)"
      >
        <div class="info-main">
          <div class="info-name">{{ info.name }}</div>
          <div class="info-details">{{ info.gender }} · {{ info.age }}岁 · {{ info.education }}</div>
          <div class="info-contact">{{ info.phone }}</div>
          <div class="info-extra" v-if="info.languagesArray && info.languagesArray.length > 0">
            语种：{{ info.languagesArray.join('、') }}
          </div>
        </div>
        <div class="info-actions">
          <button 
            class="edit-btn" 
            @click.stop="editPersonalInfo(info)"
            title="编辑信息"
          >
            ✏️
          </button>
          <div class="info-check" v-if="selectedPersonalInfo && selectedPersonalInfo.id === info.id">✓</div>
        </div>
      </div>
    </div>
    
    <!-- 无个人信息状态 -->
    <div v-else class="no-personal-info">
      <div class="no-info-icon">👤</div>
      <div class="no-info-text">暂无观众信息</div>
      <div class="no-info-hint">请添加观众信息以完成预约</div>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="personalInfoSelectorVisible = false">取消</el-button>
        <el-button 
          @click="$emit('refreshPersonalInfo')" 
          :loading="personalInfoLoading"
          icon="Refresh"
        >
          刷新信息
        </el-button>
        <!-- 只有在没有个人信息时才显示新增按钮 -->
        <el-button 
          v-if="personalInfoList.length === 0" 
          type="primary" 
          @click="goToAddPersonalInfo"
        >
          新增观众信息
        </el-button>
        <el-button 
          v-if="selectedPersonalInfo" 
          type="primary" 
          @click="confirmPersonalInfoSelection"
        >
          确认选择
        </el-button>
      </div>
    </template>
  </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage, ElDialog } from 'element-plus'

// Props
const props = defineProps({
  selectedPersonalInfo: {
    type: Object,
    default: null
  },
  personalInfoList: {
    type: Array,
    default: () => []
  },
  personalInfoLoading: {
    type: Boolean,
    default: false
  }
})

// Emits
const emit = defineEmits([
  'update:selectedPersonalInfo', 
  'refreshPersonalInfo',
  'showPersonalInfoForm',
  'editPersonalInfo'
])

// 响应式数据
const personalInfoSelectorVisible = ref(false)

// 显示个人信息选择器
const showPersonalInfoSelector = () => {
  personalInfoSelectorVisible.value = true
}

// 选择个人信息
const selectPersonalInfo = (info) => {
  emit('update:selectedPersonalInfo', info)
}

// 跳转到新增个人信息页面
const goToAddPersonalInfo = () => {
  personalInfoSelectorVisible.value = false
  emit('showPersonalInfoForm')
}

// 编辑个人信息
const editPersonalInfo = (info) => {
  personalInfoSelectorVisible.value = false
  emit('editPersonalInfo', info)
}

// 确认个人信息选择
const confirmPersonalInfoSelection = () => {
  if (!props.selectedPersonalInfo) {
    ElMessage.error('请选择个人信息')
    return
  }

  personalInfoSelectorVisible.value = false
  ElMessage.success('个人信息选择成功')
}
</script>

<style scoped>
/* 观众信息区域 */
.visitor-info-section {
  background: white;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}

.visitor-info-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.section-title {
  margin: 0;
  font-size: 16px;
  font-weight: bold;
  color: #333;
}

.required-notice {
  font-size: 12px;
  color: #666;
}

.visitor-card {
  border: 1px solid #e8eaf6;
  border-radius: 10px;
  padding: 15px;
  background: #fafafa;
  cursor: pointer;
  transition: all 0.3s ease;
}

.visitor-card:hover {
  border-color: #667eea;
  background: #f8f9ff;
}

.personal-info-prompt {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.prompt-text {
  font-size: 14px;
  color: #667eea;
}

.prompt-arrow {
  font-size: 16px;
  color: #667eea;
  font-weight: bold;
}

/* 个人信息选择器 */
:deep(.personal-info-selector-dialog) {
  border-radius: 15px;
  overflow: hidden;
}

:deep(.personal-info-selector-dialog .el-dialog__header) {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  padding: 20px;
  margin: 0;
}

:deep(.personal-info-selector-dialog .el-dialog__title) {
  color: white;
  font-weight: bold;
}

/* 通用弹窗关闭按钮样式 */
:deep(.personal-info-selector-dialog .el-dialog__headerbtn .el-dialog__close) {
  color: white;
  font-size: 18px;
  transition: all 0.3s ease;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

:deep(.personal-info-selector-dialog .el-dialog__headerbtn .el-dialog__close:hover) {
  background: rgba(255, 255, 255, 0.2);
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

:deep(.personal-info-selector-dialog .el-dialog__headerbtn) {
  top: 20px;
  right: 20px;
  width: 32px;
  height: 32px;
}

:deep(.personal-info-selector-dialog .el-dialog__body) {
  padding: 25px;
  background: linear-gradient(135deg, #f0f4ff 0%, #e8f0fe 100%);
}

.personal-info-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-height: 400px;
  overflow-y: auto;
  padding: 4px; /* 为阴影留出空间 */
}

.personal-info-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 22px;
  border: 2px solid #e3e8f5;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: white;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.08);
  position: relative;
  overflow: hidden;
}

.personal-info-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  opacity: 0;
  transition: opacity 0.3s ease;
  z-index: 1;
}

.personal-info-item:hover {
  border-color: #667eea;
  transform: translateY(-4px);
  box-shadow: 0 12px 35px rgba(102, 126, 234, 0.25);
  background: #fafbff;
}

.personal-info-item.active::before {
  opacity: 1;
}

.personal-info-item.active {
  border-color: #5a6fd8;
  color: white;
  transform: translateY(-3px);
  box-shadow: 0 12px 40px rgba(102, 126, 234, 0.4);
}

.info-main {
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: relative;
  z-index: 2;
  flex: 1;
  min-width: 0; /* 防止内容溢出 */
}

.info-name {
  font-size: 19px;
  font-weight: 700;
  margin: 0;
  letter-spacing: 0.3px;
  color: #2c3e50;
  line-height: 1.3;
}

.personal-info-item.active .info-name {
  color: white;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.info-details {
  font-size: 15px;
  color: #5a6c7d;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 10px;
  line-height: 1.4;
}

.info-details::before {
  content: '●';
  color: #667eea;
  font-size: 8px;
  opacity: 0.7;
}

.personal-info-item.active .info-details {
  color: rgba(255, 255, 255, 0.95);
}

.personal-info-item.active .info-details::before {
  color: rgba(255, 255, 255, 0.8);
}

.info-contact {
  font-size: 14px;
  color: #667eea;
  font-weight: 600;
  letter-spacing: 0.5px;
  margin-top: 2px;
}

.personal-info-item.active .info-contact {
  color: rgba(255, 255, 255, 0.9);
}

.info-extra {
  font-size: 13px;
  color: #8e99a4;
  font-weight: 500;
  margin-top: 4px;
  padding: 6px 10px;
  background: rgba(102, 126, 234, 0.05);
  border-radius: 8px;
  border-left: 3px solid #667eea;
}

.personal-info-item.active .info-extra {
  background: rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 0.85);
  border-left-color: rgba(255, 255, 255, 0.6);
}

.info-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  position: relative;
  z-index: 2;
  flex-shrink: 0;
}

.edit-btn {
  background: rgba(102, 126, 234, 0.1);
  border: 1px solid rgba(102, 126, 234, 0.3);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
  color: #667eea;
  margin-top: -3px;
  margin-right: 50px;
}

.edit-btn:hover {
  background: rgba(102, 126, 234, 0.2);
  border-color: rgba(102, 126, 234, 0.5);
  transform: scale(1.05);
}

.edit-btn:active {
  transform: scale(0.95);
}

.personal-info-item.active .edit-btn {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.3);
  color: white;
}

.personal-info-item.active .edit-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
}

.info-check {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: 3px solid white;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  margin-top: -40px;
  margin-right: -40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
  color: white;
  position: relative;
  z-index: 2;
  animation: checkScale 0.4s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
  flex-shrink: 0;
}

@keyframes checkScale {
  0% { transform: scale(0.8); }
  100% { transform: scale(1); }
}

.no-personal-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 60px 20px;
  text-align: center;
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 30px rgba(102, 126, 234, 0.1);
  border: 1px solid #e8f0fe;
}

.no-info-icon {
  font-size: 72px;
  margin-bottom: 24px;
  opacity: 0.7;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 2px 4px rgba(102, 126, 234, 0.2));
}

.no-info-text {
  font-size: 20px;
  color: #2c3e50;
  margin-bottom: 12px;
  font-weight: 700;
  letter-spacing: 0.3px;
}

.no-info-hint {
  font-size: 15px;
  color: #5a6c7d;
  line-height: 1.6;
  font-weight: 500;
}

/* 加载状态样式优化 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 60px 20px;
  text-align: center;
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 30px rgba(102, 126, 234, 0.1);
  border: 1px solid #e8f0fe;
}

.loading-spinner {
  font-size: 48px;
  margin-bottom: 20px;
  animation: spin 1.5s linear infinite;
  filter: drop-shadow(0 2px 4px rgba(102, 126, 234, 0.2));
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.loading-text {
  color: #5a6c7d;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 0.3px;
}

.dialog-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
  padding: 20px 25px;
  background: white;
  border-top: 1px solid #e8eaf6;
  margin: 0 -25px -25px -25px;
}

.dialog-footer .el-button {
  min-width: 100px;
  height: 42px;
  border-radius: 21px;
  font-weight: bold;
  transition: all 0.3s ease;
}

.dialog-footer .el-button:not(.el-button--primary) {
  background: #f5f5f5;
  border: 1px solid #ddd;
  color: #666;
}

.dialog-footer .el-button:not(.el-button--primary):hover {
  background: #eeeeee;
  border-color: #ccc;
  color: #555;
}

.dialog-footer .el-button--primary {
  background: linear-gradient(135deg, #667eea, #764ba2);
  border: none;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.dialog-footer .el-button--primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

/* 移动端适配 */
@media (max-width: 768px) {
  :deep(.personal-info-selector-dialog .el-dialog__body) {
    padding: 20px;
    background: linear-gradient(135deg, #f5f8ff 0%, #ebf2ff 100%);
  }
  
  .personal-info-list {
    gap: 14px;
    padding: 2px;
  }
  
  .personal-info-item {
    padding: 18px;
    border-radius: 14px;
    box-shadow: 0 3px 15px rgba(102, 126, 234, 0.08);
  }
  
  .personal-info-item:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.2);
  }
  
  .personal-info-item.active {
    transform: translateY(-2px);
    box-shadow: 0 8px 30px rgba(102, 126, 234, 0.35);
  }
  
  .info-main {
    gap: 8px;
  }
  
  .info-name {
    font-size: 17px;
    font-weight: 700;
  }
  
  .info-details {
    font-size: 14px;
    gap: 8px;
  }
  
  .info-contact {
    font-size: 13px;
    margin-top: 1px;
  }
  
  .info-extra {
    font-size: 12px;
    padding: 5px 8px;
    margin-top: 3px;
  }
  
  .info-check {
    width: 32px;
    height: 32px;
    font-size: 16px;
    border-width: 2px;
  }
  
  .no-personal-info {
    padding: 50px 20px;
    border-radius: 14px;
  }
  
  .no-info-icon {
    font-size: 56px;
    margin-bottom: 20px;
  }
  
  .no-info-text {
    font-size: 18px;
    margin-bottom: 10px;
    font-weight: 700;
  }
  
  .no-info-hint {
    font-size: 14px;
  }
  
  .loading-container {
    padding: 50px 20px;
    border-radius: 14px;
  }
  
  .loading-spinner {
    font-size: 40px;
    margin-bottom: 16px;
  }
  
  .loading-text {
    font-size: 15px;
  }
  
  .dialog-footer {
    padding: 15px 20px;
    flex-direction: row;
    gap: 12px;
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .dialog-footer .el-button {
    flex: 1;
    min-width: 80px;
    max-width: 120px;
    height: 40px;
    font-size: 15px;
  }
}
</style> 